#image: harbor.dtyunxi.cn/library/yunxicibase:0.1.9
#image: registry.cn-hangzhou.aliyuncs.com/yx-repo/yunxicibase:0.3.0-debian
image: harbor-gz.dtyunxi.cn/yx-repo/yunxicibase:0.3.0-debian


# 增加参数
variables:
  # xmarket开发环境上报地址
  XMARKET_URL_DEV: http://xltn-inner.yx.com/dev/api/xmarket/v1/market/artifact/upload
  # xmarket生产环境上报地址（暂时不开放，后续提供）

stages:
  - dev_build
  - docker_build


dev_build:
  stage: dev_build
  only:
    - tags
    - ci
    - /^dev.*$
  script:
    - npm install --registry https://repo.dtyunxi.cn/nexus/repository/npm
    - chmod -R 777 node_modules
    - npm run yunxi-dev

    - mkdir -p /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/dist
    - cp -r dist /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/dist
    # - zip -q -r dist.zip dist/
    # - mc cp --recursive dist.zip minio/idp/${GROUP_NAME}/${PROJECT_NAME}/${CI_COMMIT_TAG}/
    - cp -r dist docker/
    - kaniko --cache=true --cache-dir=/kaniko-cache  --skip-tls-verify=true -c $PWD/docker/ -f $PWD/docker/Dockerfile -d ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}
    - chart_build

    
docker_build:
  stage: docker_build
  only:
    - tags
    - ci
    - /^sprint.*$/
  script:
    #- npm run clean-dist
    #- yarn install --registry http://registry-hz.dtyunxi.cn/repository/taobao-npm/ --sass-binary-site=http://npm.taobao.org/mirrors/node-sass
    - npm install --registry https://repo.dtyunxi.cn/nexus/repository/npm
    - chmod -R 777 node_modules
    - npm run yunxi-demo

    #- mc config host add minio ${MINIO_URL} ${MINIO_USER} ${MINIO_PASSWORD} S3v4
    #- mc mb -p minio/idp
    #- mc policy public minio/idp
    - mkdir -p /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/dist
    - cp -r dist  $CI_PROJECT_DIR/docker/  &&  cp -r dist /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/dist
    - zip -q -r dist.zip dist/
    # - mc cp --recursive dist.zip minio/idp/${GROUP_NAME}/${PROJECT_NAME}/${CI_COMMIT_TAG}/
    # - kaniko --skip-tls-verify -c $CI_PROJECT_DIR/docker -f $CI_PROJECT_DIR/docker/Dockerfile -d ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}
    - export DOCKER_REGISTRY=registry.cn-beijing.aliyuncs.com
    - export GROUP_NAME=tsbeer
    - export DOCKER_USER=ops@1986796415329570
    - export DOCKER_PWD=ZcrNUd1jBInJEfgB
    - echo ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}
    - docker login -u ${DOCKER_USER} -p ${DOCKER_PWD} ${DOCKER_REGISTRY}
    - kaniko --skip-tls-verify -c $CI_PROJECT_DIR/docker -f $CI_PROJECT_DIR/docker/Dockerfile -d ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}
    - chart_build

.auto_devops: &auto_devops |
  http_status_code=`curl -o .auto_devops.sh -s -m 10 --connect-timeout 10 -w %{http_code} "${CHOERODON_URL}/devops/ci?token=${Token}&type=front"`
  if [ "$http_status_code" != "200" ]; then
    cat .auto_devops.sh
    exit 1
  fi
  source .auto_devops.sh

before_script:
  - *auto_devops
